package bupt.party.online.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import bupt.party.online.dao.IBranchDao;
import bupt.party.online.model.Branch;
import bupt.party.online.model.BranchNews;

public class BranchDaoImpl implements IBranchDao {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public Branch findBranchById(int id) {
		
		String queryString = "from Branch a where  a.id=" + id;
		List<Branch> list = hibernateTemplate.find(queryString);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public Branch queryBranchByBranchName(String branchname) {
		// 根据支部名字获取支部
		String queryString = "from Branch a where a.branchName='" + branchname
				+ "'";
		List<Branch> list = hibernateTemplate.find(queryString);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public void deleteBranch(Branch branch) {
		// 删除支部
		hibernateTemplate.delete(branch);

	}

	public void saveBranch(Branch branch) {
		// 保存支部
		hibernateTemplate.save(branch);
	}

	public void updateBranch(Branch branch) {
		// 更新支部
		hibernateTemplate.update(branch);
	}

	public List<Branch> queryAll() {
		// 查找所有支部
		String queryString = "from Branch";
		return hibernateTemplate.find(queryString);
	}

	/*
	 * 根据支部id号获得支部下人数
	 */
	public int getCountByBranchID(int id) {
		int count;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();

		try {
			String sql = "select count(*) from Student p where p.studentBranchId=?";
			Query query = session.createQuery(sql);
			query.setParameter(0, id);

			count = ((Long) query.uniqueResult()).intValue();

			return count;

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public List<Branch> findChildBranchesByID(int id) {
		// 根据父id发现子支部
		//wangdong  add "isDelete=0"
		String queryString = "from Branch a where   a.isDelete=0  and  a.parentId=" + id;
		List<Branch> list = hibernateTemplate.find(queryString);
		if (list.size() == 0) {
			return null;
		} else {
			return list;
		}
	}

	@Override
	public List<Branch> findParentBranchesByID() {
		// 发现所有一级支部
		//wangdong  add  "isdelete=0"
		String queryString = "from Branch a where  a.isDelete=0  and  a.parentId=1";
		List<Branch> list = hibernateTemplate.find(queryString);
		if (list.size() == 0) {
			return null;
		} else {
			return list;
		}
	}

	@Override
	public String getBranchName(int id) {

		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();

		try {
			String sql = "select branchName from Branch p where p.id=?";
			Query query = session.createQuery(sql);
			query.setParameter(0, id);
			List<Object> list = query.list();
			String s;
			if(list!=null && list.size() > 0){
				s =(String) list.get(0);
			}else{
				s="";
			}
			return s;

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public List<Branch> getResultsByPage(int id,int firstResult,
			int pageSize) {
		// TODO Auto-generated method stub
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			if(id==-2){
				String sql = "from Branch b order by b.createTime";
				Query query = session.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(pageSize);				
				return query.list();
				
			}else{
				String sql = "from Branch b where b.parentId=? order by b.createTime";
				Query query = session.createQuery(sql);
				query.setParameter(0, id);
				query.setFirstResult(firstResult);
				query.setMaxResults(pageSize);
				
				return query.list();
			}
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public int getResultCountByTag(int id) {
		// TODO Auto-generated method stub
		if(id <= 0) return 0;
		
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			String sql = "select count(*) from Branch b where b.parentId=?";
			
			Query query = session.createQuery(sql);
			query.setParameter(0, id);
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		
	}
	
	public int getCount(){
				
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			String sql = "select count(*) from Branch";
			
			Query query = session.createQuery(sql);
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean checkName(String name) {
		
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			String sql = "from Branch b where b.branchName=?";
			Query query = session.createQuery(sql);
			query.setParameter(0, name);			
			List<Branch>list = query.list();
			if(list==null || list.size() < 1){
				return false;
			}else{
				return true;
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return true;
		}
	}

	@Override
	public List<Branch> getLevel2Branches() {
		// TODO Auto-generated method stub
		//wangdong  modify it to  add “isDelete =0”
		return this.hibernateTemplate.find("from Branch b where b.isDelete=0  and  b.level=?", 2);
		
	}

}